package com.yeyi.ytest;

import java.math.BigDecimal;

import com.alibaba.fastjson.JSON;
import com.yeyi.YTool.BigDecimalUtil;

class Price{
	Double d = 1.01;
	String s = "1.01";
	BigDecimal bd = new BigDecimal(-0.91);
	
	public Double getD() {
		return d;
	}
	public void setD(Double d) {
		this.d = d;
	}
	public String getS() {
		return s;
	}
	public void setS(String s) {
		this.s = s;
	}
	public BigDecimal getBd() {
		return bd;
	}
	public void setBd(BigDecimal bd) {
		this.bd = bd;
	}
	
}

public class BigDecimalTest {
    
    /**
     * 四舍五入
     * 
     * @author: yeyi
     * @date: 2018年4月27日
     */
    static void t_Rounding() {
        BigDecimal bd1 = new BigDecimal(1.49);
        BigDecimal bd2 = new BigDecimal(1.51);
        
        System.out.println(bd1);
        System.out.println(bd2);
        
        System.out.println(BigDecimalUtil.rounding4_5(bd1));
        System.out.println(BigDecimalUtil.rounding4_5(bd2));
    }
    
	public static void t_thousandRounding4_5() {
		System.out.println(BigDecimalUtil.thousandRounding4_5(new BigDecimal(10000.123)));
		System.out.println(BigDecimalUtil.thousandRounding4_5(new BigDecimal(-10000.126)));
		System.out.println(BigDecimalUtil.thousandRounding4_5(new BigDecimal(-9999.1)));
		System.out.println(BigDecimalUtil.thousandRounding4_5(new BigDecimal(9999)));
		
		System.out.println(BigDecimalUtil.thousandRounding4_5Cut0Integer(new BigDecimal(-9999.1)));
		System.out.println(BigDecimalUtil.thousandRounding4_5Cut0Integer(new BigDecimal(9999)));
	}
	
	public static void t_rounding() {
		Price p = new Price();
		System.out.println(JSON.toJSONString(p, true));
		p.setBd(BigDecimalUtil.rounding4_5(p.getBd()));
		p.setD(BigDecimalUtil.rounding4_5(p.getD()));
		p.setS(BigDecimalUtil.rounding4_5(p.getS()));
		System.out.println(JSON.toJSONString(p, true));
	}
	
	public static void rounding4_5Cut0Integer() {
		System.out.println(BigDecimalUtil.rounding4_5Cut0Integer(new BigDecimal("1.00")));
		System.out.println(BigDecimalUtil.rounding4_5Cut0Integer(new BigDecimal("1.01")));
		System.out.println(BigDecimalUtil.rounding4_5Cut0Integer(new BigDecimal("1.30")));
	}
	
	public static void rounding4_5Cut0() {
		System.out.println(BigDecimalUtil.rounding4_5Cut0(new BigDecimal("1.00")));
		System.out.println(BigDecimalUtil.rounding4_5Cut0(new BigDecimal("1.01")));
		System.out.println(BigDecimalUtil.rounding4_5Cut0(new BigDecimal("1.30")));
		System.out.println(BigDecimalUtil.rounding4_5Cut0(new BigDecimal("130")));
	}
	
	public static void equal() {
		System.out.println(new BigDecimal(1.00).equals(1));
		System.out.println(new BigDecimal(1.01).equals(1.01));
		double d = 100000.3;
		System.out.println(new BigDecimal(1.30).equals(d)); // 注意不能直接 equals
		
		System.out.println(new BigDecimal(100000.30).doubleValue()==d);
	}
	
	public static void stripTrailingZeros() {
		System.out.println(new BigDecimal(1.30).stripTrailingZeros());
		System.out.println(new BigDecimal("1.30").stripTrailingZeros());
		System.out.println(new BigDecimal(1.00).stripTrailingZeros());
	}
    
    public static void main(String[] args) {
//        t_Rounding();
//    	equal();
    	rounding4_5Cut0();
    }
}
